package cn.itcast.bos.delivery.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import cn.itcast.bos.delivery.dao.WayBillDao;
import cn.itcast.bos.delivery.service.WayBillService;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.delivery.Order;
import cn.itcast.bos.domain.delivery.WayBill;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.elasticsearch.dao.WayBillElasticSearchDao;
import cn.itcast.bos.transit.dao.TransitInfoDao;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService{
	@Autowired
	private WayBillDao wayBillDao;
	@Autowired
	private WayBillElasticSearchDao wayBillElasticSearchDao;
	@Autowired
	private TransitInfoDao transitInfoDao;
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public void save(Order order, Courier courier) {
		// TODO Auto-generated method stub
		WayBill wayBill=new WayBill();
		wayBill.setWayBillNum(UUID.randomUUID().toString());
		wayBill.setOrder(order);
		
		wayBill.setSendName(order.getSendName());
		wayBill.setSendMobile(order.getSendMobile());
		wayBill.setSendCompany(order.getSendCompany());
		wayBill.setSendArea(order.getSendArea());
		wayBill.setSendAddress(order.getSendAddress());
		
		wayBill.setRecName(order.getRecName());
		wayBill.setRecAddress(order.getRecAddress());
		wayBill.setRecArea(order.getRecArea());
		wayBill.setRecCompany(order.getRecCompany());
		wayBill.setRecMobile(order.getRecMobile());
		
		wayBill.setRemark(order.getRemark());
		wayBill.setSendProNum(order.getSendProNum());
		wayBill.setGoodsType(order.getGoodsType());
		wayBill.setPayTypeNum(order.getPayTypeNum());
		wayBill.setWeight(order.getWeight());
		wayBill.setWayBillType("1");
		
		wayBillDao.save(wayBill);
	}
	@Override
	public void save(WayBill t) {
		// TODO Auto-generated method stub
		if(t.getId()==null||t.getId()==0){
			logger.info("运单ID为空");
			logger.info(t.toString());
			WayBill wayBill = wayBillDao.findByWayBillNum(t.getWayBillNum());
			if(wayBill!=null){
				if(wayBill.getSignStatus()==1){
					BeanUtils.copyProperties(t, wayBill, "id");            
					wayBillElasticSearchDao.save(wayBill);
					logger.info(wayBill.toString());
				}else{
					new RuntimeException();
				}
				return;
			}
			logger.info(t.getId()+t.getWayBillNum());
			t.setSignStatus(1);
			WayBill save = wayBillDao.save(t);
			wayBillElasticSearchDao.save(save);
		}
	}
	@Override
	public Page<WayBill> show(Pageable pageable) {
		// TODO Auto-generated method stub
		return wayBillDao.findAll(pageable);
	}
	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		// TODO Auto-generated method stub
		return wayBillDao.findByWayBillNum(wayBillNum);
	}
	@Override
	public Page<WayBill> searchFromElastic(Pageable pageable, WayBill t) {
		// TODO Auto-generated method stub
		BoolQueryBuilder query = extracted(t);
		Page<WayBill> search = wayBillElasticSearchDao.search(query, pageable);
		return search;
	}
	
	
	private BoolQueryBuilder extracted(WayBill t) {
		BoolQueryBuilder query=new BoolQueryBuilder();
		if(StringUtils.isNotBlank(t.getWayBillNum())){
			QueryBuilder termQueryBuilder=new TermQueryBuilder("wayBillNum",t.getWayBillNum());
			query.must(termQueryBuilder);
		}
		if(StringUtils.isNotBlank(t.getSendProNum())){
			QueryBuilder termQueryBuilder=new TermQueryBuilder("sendProNum",t.getSendProNum());
			query.must(termQueryBuilder);
		}
		if(t.getSignStatus()!=null&&t.getSignStatus()!=0){
			QueryBuilder termQueryBuilder=new TermQueryBuilder("signStatus",t.getSignStatus());
			query.must(termQueryBuilder);
		}
		if(StringUtils.isNotBlank(t.getSendAddress())){
			BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
			QueryBuilder wildcardQueryBuilder=new WildcardQueryBuilder("sendAddress","*"+t.getSendAddress()+"*");
			QueryStringQueryBuilder queryStringQueryBuilder=new QueryStringQueryBuilder(t.getSendAddress())
			.field("sendAddress").defaultOperator(Operator.AND);
			boolQueryBuilder.should(queryStringQueryBuilder).should(wildcardQueryBuilder);
			query.must(boolQueryBuilder);
		}
		if(StringUtils.isNotBlank(t.getRecAddress())){
			BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
			logger.info(t.getRecAddress());
			QueryBuilder wildcardQueryBuilder=new WildcardQueryBuilder("recAddress","*"+t.getRecAddress()+"*");
			QueryStringQueryBuilder queryStringQueryBuilder=new QueryStringQueryBuilder(t.getRecAddress())
			.field("recAddress").defaultOperator(Operator.AND);
			boolQueryBuilder.should(queryStringQueryBuilder).should(wildcardQueryBuilder);
			query.must(boolQueryBuilder);
		}
		return query;
	}
	@Override
	public void transit(List<Integer> wayBillIds) {
		// TODO Auto-generated method stub
		logger.info(wayBillIds.toString());
		List<WayBill> list = wayBillDao.getByIds(wayBillIds);
		logger.info("wayBill个数"+list.size());
		for (WayBill wayBill : list) {
			logger.info(""+wayBill.getSignStatus());
			if(wayBill.getSignStatus()==1){
				logger.info("开始产生运输配送信息");
				wayBill.setSignStatus(2);
				TransitInfo transitInfo=new TransitInfo();
				transitInfo.setWayBill(wayBill);
				transitInfo.setStatus("出入库中转");
				transitInfoDao.save(transitInfo);
			}
		}
	}
	@Override
	public void updateElastic() {
		// TODO Auto-generated method stub
		List<WayBill> findAll = wayBillDao.findAll();
		wayBillElasticSearchDao.save(findAll);
	}
	@Override
	public Iterable<WayBill> searchFromElastic(WayBill t) {
		// TODO Auto-generated method stub
		BoolQueryBuilder extracted = extracted(t);
		Iterable<WayBill> search = wayBillElasticSearchDao.search(extracted);
		return search;
	}
	

}
